package com.liuheng.manager.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liuheng.manager.service.CaptchaService;
import com.liuheng.manager.service.SysUserService;
import com.liuheng.spzx.common.exception.CustomException;
import com.liuheng.spzx.model.dto.system.LoginDto;
import com.liuheng.spzx.model.entity.system.SysMenu;
import com.liuheng.spzx.model.entity.system.SysUser;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.system.LoginVo;
import com.liuheng.spzx.model.vo.system.SysMenuVo;
import com.liuheng.spzx.model.vo.system.ValidateCodeVo;
import com.liuheng.util.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import net.bytebuddy.build.Plugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Tag(name="用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {
    @Autowired
    private SysUserService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CaptchaService captchaService;
    //用户登出
    @GetMapping("/logout")
    public  Result logout(@RequestHeader(name ="token")String token){
        service.logout(token);
      return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    //获取用户信息
    //生成图形验证码
    @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name ="token") String token){
        SysUser user = AuthContextUtil.getUser();
        return Result.build(user,ResultCodeEnum.SUCCESS);
    }
//    @GetMapping("/getUserInfo")
//    public Result<SysUser> getUserInfo(@RequestHeader(name ="token") String token){
//        SysUser user=service.getUserInfo(token);
//        return Result.build(user,ResultCodeEnum.SUCCESS);
//    }
    @Operation(summary = "生成图形验证码")
    @GetMapping(value = "/captcha")
    public Result<ValidateCodeVo> generateCaptcha(){
        ValidateCodeVo vo=captchaService.generateCaptcha();
        return Result.build(vo,ResultCodeEnum.SUCCESS);
    }
    //用户登入功能
    @Operation(summary = "用户登入方法")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginDto sysUser){
        Result result;
        String captcha = sysUser.getCaptcha();
        String codeKey = sysUser.getCodeKey();
        String inputCaptcha = (String) redisTemplate.opsForValue().get("user:login:captcha" + codeKey);
        if(!StringUtils.hasText(inputCaptcha)||!captcha.equals(inputCaptcha)){
//            return Result.build(ResultCodeEnum.VALIDATECODE_ERROR,ResultCodeEnum.VALIDATECODE_ERROR);
           throw  new CustomException(ResultCodeEnum.VALIDATECODE_ERROR);
        }
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName,sysUser.getUserName());
        SysUser user = service.getOne(queryWrapper);
        if(user==null){
           return Result.build(ResultCodeEnum.LOGIN_ERROR,ResultCodeEnum.LOGIN_ERROR.getCode(),ResultCodeEnum.LOGIN_ERROR.getMessage());
        }
        String database_password = user.getPassword();
        String in_password = sysUser.getPassword();
        String md5password= DigestUtil.md5Hex(in_password.getBytes());
        if (database_password.equals(md5password)){
            String token = UUID.randomUUID().toString().replace("-", "");
//            String jsonString = JSON.toJSONString(user);
            redisTemplate.opsForValue().set("user:login"+token,user,7, TimeUnit.DAYS);
            LoginVo loginVo = new LoginVo();
            loginVo.setToken(token);
            result= Result.build(loginVo,ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage());

        }else {
            result= Result.build(ResultCodeEnum.LOGIN_ERROR,ResultCodeEnum.LOGIN_ERROR.getCode(),ResultCodeEnum.LOGIN_ERROR.getMessage());
        }
        return result;
    }
    @GetMapping("/menus")
    public Result menus(){
        List<SysMenuVo> sysMenuVoList=service.findUserMenuList();
        return Result.build(sysMenuVoList,ResultCodeEnum.SUCCESS);
    }
}
